package com.zhoug.encrypt3demo;

import android.os.Bundle;
import android.util.Base64;
import android.widget.Button;
import android.widget.EditText;

import com.zhoug.encrypt3.AESHelper;
import com.zhoug.encrypt3.EncryptConstants;
import com.zhoug.encrypt3.IEncryptor;
import com.zhoug.encrypt3.KeyGenerator;
import com.zhoug.encrypt3.RSAHelper;
import com.zhoug.logger3.Logger;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * RSA非对称加密
 * 算法/模式/填充                16字节加密后数据长度        不满16字节加密后长度
 * AES/CBC/NoPadding             16                          不支持
 * AES/CBC/PKCS5Padding          32                          16
 * AES/CBC/ISO10126Padding       32                          16
 * AES/CFB/NoPadding             16                          原始数据长度
 * AES/CFB/PKCS5Padding          32                          16
 * AES/CFB/ISO10126Padding       32                          16
 * AES/ECB/NoPadding             16                          不支持
 * AES/ECB/PKCS5Padding          32                          16
 * AES/ECB/ISO10126Padding       32                          16
 * AES/OFB/NoPadding             16                          原始数据长度
 * AES/OFB/PKCS5Padding          32                          16
 * AES/OFB/ISO10126Padding       32                          16
 * AES/PCBC/NoPadding            16                          不支持
 * AES/PCBC/PKCS5Padding         32                          16
 * AES/PCBC/ISO10126Padding      32                          16
 *
 * @Author 35574
 * @Date 2021/7/27
 * @Description
 */
public class TextRSAActivity extends AppCompatActivity {
    private static final String TAG = ">>>TextRSAActivity";
    private Charset charset = StandardCharsets.UTF_8;
//    private Charset charset = Charset.forName("GBK");

    private EditText mEtCount;
    private EditText mEtText;
    private Button mBtn1;
    private Button mBtn2;
    private IEncryptor rsaHelper;
    private IEncryptor aesHelper;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        addListener();
        rsaHelper = new RSAHelper.Builder()
                .setBase64Flag(Base64.NO_PADDING | Base64.NO_WRAP)
                .setCharset(charset)
                .setTransformation(EncryptConstants.TRANSFORMATION_RSA_NONE_P1)
                .setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsz9ehtLYrWS7s6sN+WGVBGuvcIc7d3/FF66YPO7LSoMItc8VYRlqsPbeEEp1C3xBzWjdg/GmILpjSiFVtfcNE+tiwJ1ktABQo8LZteKsMwTnPfk/7afVJKkJd+6u9tIcaXCJQ+65O8RIE57AWlT2yJdPLqDfa9e6+WpkLAbWh8QIDAQAB")
                .setPrivateKey("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKzP16G0titZLuzqw35YZUEa69whzt3f8UXrpg87stKgwi1zxVhGWqw9t4QSnULfEHNaN2D8aYgumNKIVW19w0T62LAnWS0AFCjwtm14qwzBOc9+T/tp9UkqQl37q720hxpcIlD7rk7xEgTnsBaVPbIl08uoN9r17r5amQsBtaHxAgMBAAECgYAZogaxFLYEvgjI2ZW/0urJ+m1LzLP7kzEdlvOdxNQki/Xheg+mpWDjtY/YBk7PUXkGMzaNq0MYERlX+SL6x7HD9hxeHonunKV8ZIZB9zypxSP2tA1CSuKQSqF7Y4niBSN5XpY3wIPWPCv42yqub2aUXhao03aUtHij28sT16uWcQJBANzmTUE381DCiHdO9te2Ak9mOftuCKhZurcqK223ZwcsDrpMbkLUnralXi0y3ofDnWyv5pcBIQN7uYlytdPjB0UCQQDIRXCl7KGB6knObAGjBWT9F3PoYLA0YNNIkePJaUAHWtJGBGJErBwK28G+h9DltgZhJ7cwW2f98tAH4ACSdnS9AkAEUVgXO0G9RWAnfNqkUsFuzd+6Q2dV6xMUO3XH2jrgUAMlrxLEqaVEiqzxkzn4S2GLW1DViMcda1CwGBbnwltFAkBxUrm3afrJ5KT25S7X7EmE3Sx9JZkPSmrD+amHYsMUZlPtwrVIXotPqaIsCfYuxZ0+hJaoClEB5c/JZPtt5RGRAkAORb+XXdQDPfINDr9Kx4GrTMaI3q//s2lhlU3o2Tw6AsBhBOAv6Df0wjYSp/Cb3vDsAm8ylLkyGEMdBemjJtW6")
                .build();
        String aesKey = KeyGenerator.generate32Key();
        Logger.d(TAG, "onCreate:aesKey=" + aesKey);
        aesHelper = new AESHelper.Builder()
                .setBase64Flag(Base64.NO_WRAP)
                .setCharset(charset)
                .setTransformation(EncryptConstants.TRANSFORMATION_AES_CBC_P7)
                .setIv("1234567890123456")
                .setKey("9e6aae62a2e24edf99a2cbcebf1261a7")
                .build();

        /*HashSet<String> set = new HashSet<>();
        set.add("1111123");
        set.add("aaaa1321");
        set.add("f给对方fgd");


        SharedPreferencesWrap sp = new SharedPreferencesWrap(SpUtils.getSharedPreferences("SPWrapTest"));
        Logger.d(TAG, "boolean:" + sp.getBoolean("boolean", false));
        Logger.d(TAG, "float:" + sp.getFloat("float", 1));
        Logger.d(TAG, "int:" + sp.getInt("int", 2));
        Logger.d(TAG, "long:" + sp.getLong("long", 3));
        Logger.d(TAG, "string:" + sp.getString("string", "a"));
        Logger.d(TAG, "stringSet:" + sp.getStringSet("stringSet", null));
        SharedPreferencesWrap.Editor edit = sp.edit();
        edit.putBoolean("boolean", false)
                .putFloat("float", 123.023f)
                .putInt("int", 512)
                .putLong("long", 821231L)
                .putString("string", "gSD敢达发过的dfg")
                .putStringSet("stringSet", set)
                .apply();*/


       /* DataValidateHelper.Result validate = new DataValidateHelper()
                .nonNull("张三", "姓名")
                .nonNull("男", "性别")
                .nonNull("大学", "专业")
                .equals("在v", "在v", "2次输入的密码不一致")
                .equals(123, 123, "2次输入的验证码不一致")
                .isEmail("sdfs@qq.com", null)
                .isMobilePhoneNumber("18825686855", null)
                .isPhoneNumber("4485458", null)
                .validate();
        if (!validate.isSuccess()) {
            Logger.d(TAG, "validate:" + validate.getMessage());
        }*/

    }

    private void findViews() {
        mEtCount = findViewById(R.id.et_count);
        mEtText = findViewById(R.id.et_text);
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
    }

    private void addListener() {
        mBtn1.setOnClickListener(v -> {
            final String source = mEtText.getText().toString();
            String trim = mEtCount.getText().toString().trim();
            int maxCount = 100;
            try {
                maxCount = Integer.parseInt(trim);
            } catch (Exception e) {
                e.printStackTrace();
            }

            int count = 1;

            while (count <= maxCount) {
                final int c = count;
                new Thread(() -> {
                    String encryptString = aesHelper.encryptToBase64(source);
                    Logger.d(TAG, c + "加密后:" + encryptString);
                    String decryptString = aesHelper.decryptToString(encryptString);
                    Logger.d(TAG, c + "解密后:" + decryptString);

                  /*  String encryptString = aesHelper.encryptToBase64(source.getBytes());
                    Logger.d(TAG, c + "加密后:" + encryptString);
                    String decryptString = aesHelper.decryptToString(encryptString);
                    Logger.d(TAG, c + "解密后:" + decryptString);*/

                  /*  byte[] encrypt = aesHelper.encrypt(source.getBytes());
                    Logger.d(TAG, c + "加密后:" + encrypt.toString());
                    String decryptString = aesHelper.decryptToString(encrypt);
                    Logger.d(TAG, c + "解密后:" + decryptString);*/


                }).start();
                count++;
            }


        });

        mBtn2.setOnClickListener(v -> {
            final String source = mEtText.getText().toString();
            String trim = mEtCount.getText().toString().trim();
            int maxCount = 100;
            try {
                maxCount = Integer.parseInt(trim);
            } catch (Exception e) {
                e.printStackTrace();
            }

            int count = 1;

            while (count < maxCount) {
                final int c = count;
                new Thread(() -> {
                    String encryptString = rsaHelper.encryptToBase64(source);
                    Logger.d(TAG, c + "加密后:" + encryptString);
                    String decryptString = rsaHelper.decryptToString(encryptString);
                    Logger.d(TAG, c + "解密后:" + decryptString);

                 /*   String encryptString = rsaHelper.encryptToBase64(source.getBytes());
                    Logger.d(TAG, c + "加密后:" + encryptString);
                    String decryptString = rsaHelper.decryptToString(encryptString);
                    Logger.d(TAG, c + "解密后:" + decryptString);*/

                   /* byte[] encrypt = rsaHelper.encrypt(source.getBytes());
                    Logger.d(TAG, c + "加密后:" + encrypt.toString());
                    String decryptString = rsaHelper.decryptToString(encrypt);
                    Logger.d(TAG, c + "解密后:" + decryptString);*/

                }).start();
                count++;
            }


        });
    }
}
